package com.maya.controller;

import javax.servlet.http.HttpSession;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.maya.model.Question;
import com.maya.model.User;
import com.maya.service.QuestionService;
import com.maya.service.UserService;


@Controller

public class AnswerQuestionController {
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private UserService userService;
	
	
	
	@RequestMapping(value = "/answerquestion",method=RequestMethod.POST)
	public String process(String answer, String qid,HttpSession session ) {
		
		User user = (User)session.getAttribute("currUser");
		if(null==user) return "";
       
        System.out.println(qid);
        System.out.println("log:提交的回答->" + answer);
        
        Question question = questionService.get(new ObjectId(qid));
        //ask 应该不为空
        System.out.println(question.getTitle());
        
        if(null != answer && answer.length() >0) {
        	userService.answer(user,question, answer);
                       
            return "redirect:/viewquestion?qid=" + qid;
        }
        
        return "redirect:/viewquestion?qid=" + qid;
        
    }
}
